#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <libssh/sftp.h>

#include <QFileDialog>
#include <QDir>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    //判断是否连接成功
    QObject::connect(&m_com, &QSshSocket::connected,  []() {
        qDebug()<<"connect sucessful";
    });

    //判断断开连接状态
    QObject::connect(&m_com, &QSshSocket::disconnected,  []() {
        qDebug()<<"disconnected";
    });

    //判断是否登陆成功
    QObject::connect(&m_com, &QSshSocket::loginSuccessful,  []() {
        qDebug()<<"login sucessful";
    });

    //显示命令执行完的结果
    QObject::connect(&m_com, &QSshSocket::commandExecuted,  [](QString command,QString response) {
        qDebug()<< "from sesrver: " << response;
    });

    //接收错误信息
    QObject::connect(&m_com, &QSshSocket::error,  [](QSshSocket::SshError error) {
        qDebug()<<error;
    });

    QObject::connect(&m_com, &QSshSocket::pushSuccessful, [](QString localFile, QString remoteFile) {
        qDebug()<< "push successful";
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    //断开连接
    m_com.disconnectFromHost();
}


void MainWindow::on_fileBtn_clicked()
{
    QFileDialog fileD = QFileDialog();
        //设置可以打开任何文件
        fileD.setFileMode(QFileDialog::AnyFile);
        //文件过滤
        fileD.setFilter(QDir::Files);

     if (fileD.exec())
     {
         QString fileName = fileD.selectedFiles()[0];
         QFileInfo fileinfo = QFileInfo(fileName);
         QString name = fileinfo.baseName();
         m_com.pushFile(fileName, "/usr/share/nginx/html/new.txt");
     }

}


void MainWindow::on_connectBtn_clicked()
{
    QString ip = ui->IPlineE->text();
    int port = ui->portlineE->text().toInt();
    //服务器ip和端口
    m_com.connectToHost(ip, port);

}


void MainWindow::on_loginBtn_clicked()
{
    QString user = ui->userlineE->text();
    QString pwd = ui->pwdlineE->text();
    //用户名和密码
    m_com.login(user, pwd);


    //要执行的命令：返回云服务器名
    m_com.executeCommand("uname -a");
}

